const PHONE_REG = /^1\d{10}$/

const LINK_REG = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/
const NUMBERING_REG = /^[0-9]*$/

function notNumberOrString (text: string | number):boolean {
  return !(
    typeof text === 'string' ||
    typeof text === 'number'
  )
}

/**
 * 校验手机号码
 * @param phone 手机号码
 */
export function checkPhone (phone: string | number): boolean {
  if (!phone) return false
  if (notNumberOrString(phone)) return false
  return PHONE_REG.test(String(phone))
}

/**
 * 校验链接
 * @param link 链接
 */
export function checkLink (link: string): boolean {
  if (!link) return false
  if (
    link.length > 300 ||
    typeof link !== 'string'
  ) return false
  return LINK_REG.test(link)
}

/**
 * 校验编号
 * @param numbering 编号
 * @param minLen 最少长度
 * @param maxlen 最大长度
 */
export function checkNumbering (
  numbering: string | number,
  minLen = 1,
  maxlen = Infinity
): boolean {
  if (!numbering && numbering !== 0) return false
  if (notNumberOrString(numbering)) return false
  numbering = String(numbering)
  if ((minLen && numbering.length < minLen) || (maxlen && numbering.length > maxlen )) return false
  return NUMBERING_REG.test(numbering)
}

/**
 * 校验字符串字节长度
 * @param text 字符串
 * @param len 字符串长度
 */
export function checkByteLenght (
  text: string | number,
  len: number
): boolean {
  if (!text) return false
  if (notNumberOrString(text)) return false
  if (!len || len <= 0) {
    console.warn('长度必须填写')
    return false
  }
  text = String(text).replace(/[\u4e00-\u9fa5]/g, 'aa')
  return text.length <= len
}
